package com.xm.servlet.reception_desk.product;

import com.xm.entity.Address;
import com.xm.entity.Commodity;
import com.xm.service.reception_desk.AddressService;
import com.xm.service.reception_desk.CommodityDetailsService;
import com.xm.service.reception_desk.CommodityParameterService;
import com.xm.service.reception_desk.CommodityService;
import com.xm.util.DecryptExampleUtil;
import lombok.SneakyThrows;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * 商品详情商品查询
 *
 * @author 吴添文
 * @date 2023-12-20 上午 11:34
 */
@WebServlet("/product/details")
public class ProductDetailsServlet extends HttpServlet {
    /**
     * 商品
     */
    private final CommodityService service = new CommodityService();
    /**
     * 地址
     */
    private final AddressService addressService = new AddressService();
    /**
     * 商品详情
     */
    private final CommodityDetailsService commodityDetailsService = new CommodityDetailsService();
    /**
     * 商品参数
     */
    private final CommodityParameterService commodityParameterService = new CommodityParameterService();

    /**
     * 商品详细页面
     *
     * @param req  an {@link HttpServletRequest} object that
     *             contains the request the client has made
     *             of the servlet
     * @param resp an {@link HttpServletResponse} object that
     *             contains the response the servlet sends
     *             to the client
     */
    @SneakyThrows(Exception.class)
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
        Integer menu = req.getParameter("menu") == null ? 1 : Integer.parseInt(req.getParameter("menu"));
        //获取所有参数 并且封装在Map集合
        Map<String, String[]> parameterMap = req.getParameterMap();
        HttpSession session = req.getSession(false);
        // 获取会话中存储的CSRF令牌
        String sessionToken = null;
        try {
            sessionToken = (String) session.getAttribute("token");
        } catch (Exception e) {
            System.out.println("没有登陆");
        }
        //AES解密
        String informationId = DecryptExampleUtil.decrypt(parameterMap.get("informationId")[0].trim());
        Integer productOverview = 2;
        Integer productParameter = 3;
        System.out.println("解密结果：" + informationId);
        // 验证CSRF令牌
        if (sessionToken != null) {
            //商品详情

            Address addressesByInformationIdAndSelected = addressService.
                    getAddressesByInformationIdAndSelected(Integer.parseInt(informationId));
            System.out.println(addressesByInformationIdAndSelected);
            if (addressesByInformationIdAndSelected == null) {
                addressesByInformationIdAndSelected = new Address();
                addressesByInformationIdAndSelected.setAddressProvince("北京");
                addressesByInformationIdAndSelected.setAddressCity("北京市");
                addressesByInformationIdAndSelected.setAddressDistrict("海淀区");
            }
            req.setAttribute("address", addressesByInformationIdAndSelected);
            req.setAttribute("accountId", parameterMap.get("accountId")[0]);
            req.setAttribute("name", parameterMap.get("name")[0]);
            req.setAttribute("id", Integer.parseInt(informationId));
            // 令牌失效
            req.setAttribute("status", true);

        } else {
            System.out.println(parameterMap.get("accountId")[0]);
            Address address = new Address();
            address.setAddressProvince("北京");
            address.setAddressCity("北京市");
            address.setAddressDistrict("海淀区");
            req.setAttribute("address", address);
            req.setAttribute("accountId", "0");
            req.setAttribute("name", "null");
            req.setAttribute("id", -1);
            // 令牌失效
            req.setAttribute("status", false);
            System.out.println("CSRF令牌验证失败");
        }

        Commodity commodity = service.searchByCommodityId(Integer.parseInt(parameterMap.get("commodityId")[0]));
        req.setAttribute("commodity", commodity);
        req.setAttribute("isCommodityImags", commodity.getCommodityImages().contains(","));

        if (menu.equals(productOverview)) {
            req.setAttribute("commodityDetails",
                    commodityDetailsService.getCommodityDetailsById(
                            Integer.parseInt(parameterMap.get("commodityId")[0])));
        }else if (menu.equals(productParameter)){
            System.out.println("------------");
            req.setAttribute("commodityParameterList",
                    commodityParameterService.
                            getCommodityParametersByCommodityId(
                                    Integer.parseInt(parameterMap.get("commodityId")[0])));
        }
        req.setAttribute("menu", menu);
        req.getRequestDispatcher("/WEB-INF/page/reception_desk/product_details.jsp").forward(req, resp);
    }
}
